home *** CD-ROM | disk | FTP | other *** search
/ CD Actual 85 / CD Temático 40 Febrero 2004.iso / DOS / ntfs / user / ntdump.c < prev    next >
Encoding:
C/C++ Source or Header  |  2001-02-15  |  4.6 KB  |  184 lines

  1. /*
  2.  *  ntdump.c
  3.  *
  4.  *  Copyright (C) 1995-1997, 1999 Martin von L÷wis
  5.  *  Copyright (C) 1997 RΘgis Duchesne
  6.  */
  7.  
  8. #ifdef HAVE_CONFIG_H
  9. #include "config.h"
  10. #endif
  11.  
  12. #include "ntfstypes.h"
  13. #include "struct.h"
  14. #include "util.h"
  15. #include "super.h"
  16. #include "nttools.h"
  17. #include "inode.h"
  18. #include "dump.h"
  19. #include <stdio.h>
  20. #include <stdlib.h>
  21. #ifdef HAVE_GETOPT_H
  22. #include <getopt.h>
  23. #else
  24. #define getopt_long(a,v,o,ol,x)        getopt(a,v,o)
  25. #endif
  26. #ifdef HAVE_UNISTD_H
  27. #include <unistd.h>
  28. #endif
  29.  
  30. char *short_opts="vrdf:o:c:Mi:ID:B:A:N:nhV";
  31. #ifdef HAVE_GETOPT_H
  32. struct option options[]={
  33.     {"raw",0,0,'r'},
  34.     {"filesystem",1,0,'f'},
  35.     {"offset",1,0,'o'},
  36.     {"cluster",1,0,'c'},
  37.     {"mft",0,0,'M'},
  38.     {"inode",1,0,'i'},
  39.     {"dir",0,0,'d'},
  40.     {"info",0,0,'I'},
  41.     {"decompress",1,0,'D'},
  42.     {"verbose",0,0,'v'},
  43.     {"bias",1,0,'B'},
  44.     {"attribute-type",1,0,'A'},
  45.     {"attribute-name",1,0,'N'},
  46.     {"linode",0,0,'n'},
  47.     {"help",0,0,'h'},
  48.     {"version",0,0,'V'},
  49.     {0,0,0,0}
  50. };
  51. #endif
  52.  
  53. void usage(void)
  54. {
  55.     fprintf(stderr,"ntdump: prints low-level structures of an NTFS\n"
  56.         "  --filesystem, -f device  Use device\n"
  57.         "  --raw, -r                Access the raw device\n"
  58.         "  --offset, -o n           Start at offset o\n"
  59.         "  --cluster, -c n          Start at cluster n\n"
  60.         "  --mft, -M                Display as master file table record\n"
  61.         "  --inode, -i n            Display inode n\n"
  62.         "  --dir, -d                Display as directory\n"
  63.         "  --info, -I               Display file system information\n"
  64.         "  --decompress, -D n       Decompress run n\n"
  65.         "  --bias, -B n             Add partition bias n [bytes]\n"
  66.         "  --attribute-type, -A n   Dump type n\n"
  67.         "  --attribute-name, -N str Dump attribute named str\n"
  68.         "  --verbose, -v            decompress verbose\n"
  69.         "  --linode, -n             show internal inode representation\n"
  70.         "  --version, -V            print version number\n"
  71.         );
  72. }
  73.  
  74. int main(int argc,char *argv[])
  75. {
  76.     int c,with_ino,error;
  77.     int raw=0,as_mft=0,as_dir=0,use_ino=0,get_info=0;
  78.     int decompress=0,verbose=0,as_inode=0;
  79.     unsigned long inode = 0, cluster = 0, bias = 0;
  80.     unsigned long attribute_type = 0x80; /* DATA */
  81.     char *attribute_name=0;
  82.     char *device=0;
  83.     ntfs_size_t offset=0;
  84.     ntfs_volume *volume;
  85.     extern int optind,opterr;
  86.     extern char *optarg;
  87.  
  88.     opterr=1;
  89.     while((c=getopt_long(argc,argv,short_opts,options,NULL))>0)
  90.         switch(c)
  91.         {
  92.         case 'r': raw=1;break;
  93.         case 'f': device=optarg;break;
  94.         case 'o': offset=strtoul(optarg,NULL,0);break;
  95.         case 'c': cluster=strtoul(optarg,NULL,0);break;
  96.         case 'M': as_mft=1;break;
  97.         case 'i': inode=strtoul(optarg,NULL,0);use_ino=1;break;
  98.         case 'd': as_dir=1;break;
  99.         case 'I': get_info=1;break;
  100.         case 'D': decompress=1;offset=strtoul(optarg,NULL,0);break;
  101.         case 'v': verbose=1;break;
  102.         case 'B': bias=strtoul(optarg,NULL,0);break;
  103.         case 'A': attribute_type=strtoul(optarg,NULL,0);break;
  104.         case 'N': attribute_name=optarg;break;
  105.         case 'n': as_inode=1;break;
  106.         case 'V': printf("ntdump " NTFS_VERSION "\n");exit(0);break;
  107.         default:
  108.             usage();
  109.             exit(1);
  110.         }
  111.     with_ino = use_ino || get_info;
  112.     volume=ntfs_open_volume(device,bias,0,!with_ino);
  113.     if(!volume && !raw)return 1;
  114.     if(!volume){
  115.         extern ntfs_volume* the_vol;
  116.         volume = the_vol;
  117.     }
  118.     if(cluster)
  119.         offset=volume->clustersize*cluster;
  120.     if(use_ino){
  121.         ntfs_inode ino;
  122.         ntfs_init_inode(&ino,volume,inode);
  123.         if(as_mft){
  124.             list_attr_mem(volume,ino.attr);
  125.             return 0;
  126.         }
  127.         if(as_dir){
  128.             dumpdir(&ino);
  129.             return 0;
  130.         }
  131.         if(as_inode){
  132.             dump_inode(&ino);
  133.             return 0;
  134.         }
  135.         if(decompress){
  136.             dump_decompress(&ino,offset,verbose);
  137.             return 0;
  138.         }
  139.         while(1)
  140.         {
  141.             char buf[4096];
  142.             ntfs_io io;
  143.  
  144.             io.fn_put=ntfs_put;
  145.             io.fn_get=0;
  146.             io.param=buf;
  147.             io.size=volume->clustersize;
  148.             error=ntfs_read_attr(&ino,attribute_type,
  149.                          attribute_name,offset,&io);
  150.             if(error)return 0;
  151.             dump_mem(buf,offset,io.size);
  152.             offset+=io.size;
  153.             if(io.size<volume->clustersize)return 0;
  154.         }
  155.         return 0;
  156.     }
  157.     if(raw)
  158.     {
  159.         dump(volume,offset+bias,offset,-1);
  160.     }else if(as_mft)
  161.     {
  162.         list_attributes(volume,offset);
  163.     }else if(get_info)
  164.     {
  165.         long volsize;
  166.         printf("Blocksize: %x\nClustersize: %x\nMFT size: %x\n",
  167.                volume->blocksize,volume->clustersize,
  168.                volume->mft_recordsize);
  169.         if (ntfs_get_volumesize(volume, &volsize) == 0)
  170.             printf("Total clusters: %ld\n",volsize);
  171.         printf("Free clusters:  %d\n",
  172.                ntfs_get_free_cluster_count(volume->bitmap));
  173.     }else{
  174.         usage();
  175.     }
  176.     return 0;
  177. }
  178.  
  179. /*
  180.  * Local variables:
  181.  * c-file-style: "linux"
  182.  * End:
  183.  */
  184.